package org.footballmanager.pandafootball.common.util;

import lombok.extern.slf4j.Slf4j;
import org.footballmanager.pandafootball.system.domain.FootYqts;
import org.footballmanager.pandafootball.system.service.impl.RedisService;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

@Slf4j
public class RandomNumber {
	private RandomNumber(){}

	/**
	 * 随机排序
	 * @param list 要进行随机排序的数据集合
	 * @return 排序结果
	 */
	public static List<String> getList(List<String> list){
		List<String> result = new ArrayList<>();
		//用一个LinkedList作为中介
		LinkedList<String> temp = new LinkedList<>();
		//初始化temp
		for(int i = 0; i < list.size(); i++){
			temp.add(i, list.get(i));
		}
		//取数
		Random rand = new Random();
		for(int i = 0; i < list.size(); i++){
			int num = rand.nextInt(list.size() - i);
			result.add(temp.get(num));
			temp.remove(num);
		}
		return result;
	}
	
	public static String getRandomNumber(){
		Random rand = new Random();
		int num = rand.nextInt(100);
		String result;
		if(num >= 0 && num <= 70){
            result = processChip(rand, 4, "大众/白人");
		}else if(num > 70 && num <= 85){
            result = processChip(rand, 6, "普通球星");
		}else if(num > 85 && num <= 95){
            result = processChip(rand, 8, "洲际球星");
		}else if(num > 95 && num < 99){
            result = processChip(rand, 10, "超级球星");
		}else if(num >= 99){
            result = processChip(rand, 20, "顶级球星");
		}else{
            result = processChip(rand, 4, "大众/白人");
		}
		return result;
	}

    private static String processChip(Random rand, Integer bound, String chipName) {
        int rnum = rand.nextInt(bound);
        return rnum == 0 ? chipName : chipName + "碎片";
    }

	public static int randomNumber(){
		Random r = new Random();
		return r.nextInt(100) + 1;
	}

	public static String randomStrByNum(Integer num){
        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < num; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
	}

	public static String randomContent(RedisService redisService){
        List<FootYqts> list = (List<FootYqts>)redisService.get("listyqts");
        if(list != null && !list.isEmpty()){
            Random r = new Random();
            FootYqts yq = list.get(r.nextInt(list.size() - 1));
            if(null != yq && !StringUtils.isEmpty(yq.getContent())){
                return yq.getContent();
            }
        }
        return "熊猫足球分为常规赛和季后赛哦";
    }

	public static void main(String[] args) {
		List<String> list = new ArrayList<>();
		for(Integer i = 0; i < 9; i++){
			list.add(i.toString());
		}
		List<String> result = RandomNumber.getList(list);
		for(String str: result){
			log.info(str);
		}
		
		for(int i = 0; i < 10; i++){
			log.info(RandomNumber.getRandomNumber() + ",");
		}
		
	}
}
